#!/usr/bin/python
# -*- coding: utf-8 -*-
# @author  : ganuoxun
# @description : 明细层api

import datetime
from fastapi import APIRouter, Body
from apps.models.QueryModel import QuerySchema
from apps.models.ResponseModel import success, error
from apps.models.BaseModel import UpdateDwdModel
from apps.database import save_dwd, get_dwd

router = APIRouter()


@router.post("/get")
async def get(query: QuerySchema = Body(...)):
    try:
        return await get_dwd(query.get_filter(),
                             query.get_projection(),
                             query.get_sort_by(),
                             query.skip,
                             query.limit)
    except Exception as e:
        print(e)
        return error(501, repr(e))


@router.post("/save")
async def save(dwd_date: UpdateDwdModel = Body(...)):
    try:
        dwd_date.latest_date = datetime.datetime.now().strftime("%Y%m%d%H%M%S%f")
        return await save_dwd({'_id': dwd_date.m_id}, dwd_date.get_dict())
    except Exception as e:
        print(e)
        return error(501, repr(e))
